﻿using Microsoft.Research.DynamicDataDisplay;
using Microsoft.Research.DynamicDataDisplay.DataSources;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Media;

namespace dll_DynamicDataDisplay.ViewModels.DispElement
{
    /// <summary>
    /// 图表元素-曲线类
    /// </summary>
    [Serializable]
    public class Vm_Element_Curve : Vm_Element
    {
        #region NotifyProperty

        /// <summary>
        /// 数据点
        /// </summary>
        public Point[] Points
        {
            get { return _Points; }
            set
            {
                if (_Points != value)
                {
                    _Points = value;
                    this.OnPropertyChanged("Points");

                    //重绘图表
                    this.ReDraw();
                }
            }
        }
        private Point[] _Points = new Point[0];

        #endregion

        #region Methods

        /// <summary>
        /// 连接到ChartPlotter对象
        /// </summary>
        /// <param name="Plotter"></param>
        public override void ChartPlotter_Attach(ChartPlotter Plotter)
        {
            base.ChartPlotter_Attach(Plotter);

            if (this.line != null)
            {
                this.Plotter.Children.Remove(this.line);
                this.line = null;
            }

            this.Points_Tmp = new List<Point>();
            this.DataSource_Point = new EnumerableDataSource<Point>(this.Points_Tmp);
            this.DataSource_Point.SetXYMapping(pt => pt);

            this.line = this.Plotter.AddLineGraph(this.DataSource_Point,
                new Pen(this.Stroke_Brush, this.Stroke_Thickness),
                new PenDescription(this.Name));

            //重绘图案
            this.ReDraw();
        }

        /// <summary>
        /// 断开与ChartPlotter的连接
        /// </summary>
        public override void ChartPlotter_Detach()
        {
            base.ChartPlotter_Detach();
        }



        /// <summary>
        /// 刷新图表数据
        /// </summary>

        public override void ReDraw()
        {
            if (this.Plotter == null)
                return;

            if (this.Points == null)
                return;

            this.Points_Tmp.Clear();
            this.Points_Tmp.Capacity = this.Points.Length;
            foreach (var p in this.Points)
            {
                this.Points_Tmp.Add(p);
            }

            //updata
            this.DataSource_Point.RaiseDataChanged();
        }

        
        #endregion

        #region Private

        /// <summary>
        /// 数据源
        /// </summary>
        [field:NonSerialized]
        EnumerableDataSource<Point> DataSource_Point;

        /// <summary>
        /// 数据缓存
        /// </summary>
        [field: NonSerialized]
        public List<Point> Points_Tmp;

        /// <summary>
        /// 曲线对象
        /// </summary>
        [field: NonSerialized]
        LineGraph line;

        #endregion
    }
}
